#include "VideoFrame.h"

extern "C"
{
    #include <libavutil/frame.h>
}


VideoFrame::VideoFrame(AVFrame* frame,int64_t ptsMs)
    :m_frame(frame),m_ptsMs(ptsMs)
{

}

VideoFrame::~VideoFrame()
{
    release();
}
//移动拷贝构造
VideoFrame::VideoFrame(VideoFrame&& other) noexcept
    :m_frame(other.m_frame),m_ptsMs(other.m_ptsMs)
{
    other.m_frame = nullptr;  
    other.m_ptsMs = 0;
}

//移动赋值重载
VideoFrame& VideoFrame::operator=(VideoFrame&& other) noexcept
{
    if(this != &other)
    {
        release();
        m_frame = other.m_frame;
        m_ptsMs = other.m_ptsMs;
        other.m_frame = nullptr; 
        other.m_ptsMs = 0;
    }
    return *this;
}


//判断是否有效
bool VideoFrame::isValid() const
{
    return m_frame != nullptr;
}

//获取原始AVFrame指针
AVFrame* VideoFrame::getRawFrame() const
{
    return m_frame;
}

//获取宽度
int VideoFrame::getWidth() const
{
    return m_frame? m_frame->width : 0;
}

//获取高度
int VideoFrame::getHeight() const
{
    return m_frame? m_frame->height : 0;
}

//获取像素格式
int VideoFrame::getPixelFormat() const
{
    return m_frame? m_frame->format : -1;
}

//获取时间戳
int64_t VideoFrame::getPtsMs() const
{
    return m_ptsMs;
}

//设置时间戳
void VideoFrame::setPtsMs(int64_t ptsMs)
{
    m_ptsMs = ptsMs;
}

//获取视频数据指针数组
uint8_t** VideoFrame::getData() const
{
    return m_frame ? m_frame->data : nullptr;
}

//获取行跨度
int* VideoFrame::getLinesize() const
{
    return m_frame ? m_frame->linesize : nullptr;
}

//释放资源
void VideoFrame::release()
{
    if(m_frame)
    {
        av_frame_free(&m_frame);  // av_frame_free 会自动调用 av_frame_unref
        m_frame = nullptr;
    }

    m_ptsMs = 0;
}





